package com.jy.blog.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jy.blog.dao.CommentDao;
import com.jy.blog.model.Comment;
import com.jy.blog.utils.DBHelper;

public class CommentDaoImpl  implements CommentDao{
	private Connection conn;
	private PreparedStatement pstm;
	private ResultSet rs;
	@Override
	public List<Comment> selectAllComment() {
		List<Comment> list=new ArrayList<Comment>();
		conn=DBHelper.getConnection();
		String sql="SELECT co_id,co_content,co_date FROM comment ";
		   try {
			pstm=conn.prepareStatement(sql);
			rs=pstm.executeQuery();
			while(rs.next()){
				Comment comment=new Comment();
				comment.setCoId(rs.getInt(1));
				comment.setCoContent(rs.getString(2));
				comment.setCoDate(rs.getString(3));
				list.add(comment);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		   DBHelper.closeAll(pstm, rs);
		}
		   return list;
	}

	@Override
	public int Comdel(int id) {
		int result=0;
		conn=DBHelper.getConnection();
		String sql="DELETE FROM comment WHERE co_id=?";
		  try {
			pstm=conn.prepareStatement(sql);
			pstm.setInt(1, id);
			result=pstm.executeUpdate();
			
					
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		return result;
	}

}
